using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Waaahsabi.Controls.TheGame
{
	public class ThreadControl : Control
	{
		private Button _btnCancel;
		private Button _btnReply;
		private Button _btnSend;
		private bool _isFollowUpsExpanded;
		private bool _isMessageExpanded;
		private TextBox _txtBody;
		private TextBox _txtSubject;

		#region NewSubject DependencyProperty

		public static readonly DependencyProperty NewSubjectProperty =
			DependencyProperty.Register
				(
					"NewSubject",
					typeof (string),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public string NewSubject
		{
			get { return (string) GetValue(NewSubjectProperty); }
			set { SetValue(NewSubjectProperty, value); }
		}

		#endregion

		#region NewBody DependencyProperty

		public static readonly DependencyProperty NewBodyProperty =
			DependencyProperty.Register
				(
					"NewBody",
					typeof (string),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public string NewBody
		{
			get { return (string) GetValue(NewBodyProperty); }
			set { SetValue(NewBodyProperty, value); }
		}

		#endregion

		public ThreadControl()
		{
			DefaultStyleKey = typeof (ThreadControl);
			PostFollowUps = new ObservableCollection<ThreadControl>();
		}

		public bool IsComposing { get; set; }

		public event EventHandler SubmitMessage;
		public event EventHandler ComposingCanceled;
		public event EventHandler Reply;
		public event EventHandler GetFollowUps;
		public event EventHandler Rendered;

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			_btnReply = GetTemplateChild("btnReply") as Button;
			if (_btnReply != null)
			{
				_btnReply.Click += BtnReplyClick;
			}

			_btnSend = GetTemplateChild("btnSend") as Button;
			if (_btnSend != null)
			{
				_btnSend.Click += BtnSendClick;
			}

			_btnCancel = GetTemplateChild("btnCancel") as Button;
			if (_btnCancel != null)
			{
				_btnCancel.Click += BtnCancelClick;
			}
			_txtBody = GetTemplateChild("txtBody") as TextBox;
			_txtSubject = GetTemplateChild("txtSubject") as TextBox;
			if (IsComposing)
				VisualStateManager.GoToState(this, "Composing", false);

			if (IsMessageExpanded)
			{
				VisualStateManager.GoToState(this, "Expanded", true);
				InvalidateMeasure();
				InvalidateArrange();
			}

			if (IsSelected)
				SetSelected();
		}

		protected override Size ArrangeOverride(Size finalSize)
		{
			try
			{
				return base.ArrangeOverride(finalSize);
			}
			finally
			{
				if (Rendered != null)
					Rendered(this, null);
			}
		}

		protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonUp(e);
			if (GetFollowUps != null)
				GetFollowUps(this, null);
		}

		private void BtnCancelClick(object sender, RoutedEventArgs e)
		{
			if (ComposingCanceled != null)
				ComposingCanceled(this, null);
			NewBody = "";
			NewSubject = "";
			VisualStateManager.GoToState(this, "Expanded", true);
		}

		private void BtnSendClick(object sender, RoutedEventArgs e)
		{
			NewSubject = _txtSubject.Text;
			NewBody = _txtBody.Text;
			if (SubmitMessage != null)
				SubmitMessage(this, null);
			VisualStateManager.GoToState(this, "Expanded", true);
		}

		private void BtnReplyClick(object sender, RoutedEventArgs e)
		{
			if (Reply != null)
				Reply(this, null);
			//ThreadControl tc = new ThreadControl();
			//tc.PostRootId = PostRootId == 0 ? PostId : PostRootId;
			//tc.PostReplyToId = PostId;
			//tc.NewSubject = string.Format("Re: {0}", PostSubject);
			//tc.NewBody = string.Format("\n\n[Quote]\nFrom: {0}\nOn: {1}\n\n{2}\n[/Quote]", PostFrom, PostTimeStamp, PostBody);
			//tc.IsComposing = true;
			//tc.SubmitMessage += (_s, _e) => { if (SubmitMessage != null) SubmitMessage(_s, null); };
			//if(!isFollowUpsExpanded)
			//{
			//    VisualStateManager.GoToState(this, "ThreadExpanded", true);
			//    isFollowUpsExpanded = true;
			//}
			//PostFollowUps.Insert(0, tc);
		}

		public void DisableQuoteButton()
		{
			_btnReply.IsEnabled = false;
		}

		public void EnableQuoteButton()
		{
			_btnReply.IsEnabled = true;
		}

		public void SetSelected()
		{
			VisualStateManager.GoToState(this, "MessageSelected", true);
		}

		#region PostHasFollowUps DependencyProperty

		public static readonly DependencyProperty PostHasFollowUpsProperty =
			DependencyProperty.Register
				(
					"PostHasFollowUps",
					typeof (bool),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public bool PostHasFollowUps
		{
			get { return (bool) GetValue(PostHasFollowUpsProperty); }
			set { SetValue(PostHasFollowUpsProperty, value); }
		}

		#endregion

		#region PostSubject DependencyProperty

		public static readonly DependencyProperty PostSubjectProperty =
			DependencyProperty.Register
				(
					"PostSubject",
					typeof (string),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public string PostSubject
		{
			get { return (string) GetValue(PostSubjectProperty); }
			set { SetValue(PostSubjectProperty, value); }
		}

		#endregion

		#region PostBody DependencyProperty

		public static readonly DependencyProperty PostBodyProperty =
			DependencyProperty.Register
				(
					"PostBody",
					typeof (string),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public string PostBody
		{
			get { return (string) GetValue(PostBodyProperty); }
			set { SetValue(PostBodyProperty, value); }
		}

		#endregion

		#region PostFrom DependencyProperty

		public static readonly DependencyProperty PostFromProperty =
			DependencyProperty.Register
				(
					"PostFrom",
					typeof (string),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public string PostFrom
		{
			get { return (string) GetValue(PostFromProperty); }
			set { SetValue(PostFromProperty, value); }
		}

		#endregion

		#region PostId DependencyProperty

		public static readonly DependencyProperty PostIdProperty =
			DependencyProperty.Register
				(
					"PostId",
					typeof (ulong),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public ulong PostId
		{
			get { return (ulong) GetValue(PostIdProperty); }
			set { SetValue(PostIdProperty, value); }
		}

		#endregion

		#region PostTimeStamp DependencyProperty

		public static readonly DependencyProperty PostTimeStampProperty =
			DependencyProperty.Register
				(
					"PostTimeStamp",
					typeof (DateTime),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public DateTime PostTimeStamp
		{
			get { return (DateTime) GetValue(PostTimeStampProperty); }
			set { SetValue(PostTimeStampProperty, value); }
		}

		#endregion

		#region PostFollowUps DependencyProperty

		public static readonly DependencyProperty PostFollowUpsProperty =
			DependencyProperty.Register
				(
					"PostFollowUps",
					typeof (ObservableCollection<ThreadControl>),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public ObservableCollection<ThreadControl> PostFollowUps
		{
			get { return (ObservableCollection<ThreadControl>) GetValue(PostFollowUpsProperty); }
			set { SetValue(PostFollowUpsProperty, value); }
		}

		#endregion

		#region PostReplyToId DependencyProperty

		public static readonly DependencyProperty PostReplyToIdProperty =
			DependencyProperty.Register
				(
					"PostReplyToId",
					typeof (ulong),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public ulong PostReplyToId
		{
			get { return (ulong) GetValue(PostReplyToIdProperty); }
			set { SetValue(PostReplyToIdProperty, value); }
		}

		#endregion

		#region PostRootId DependencyProperty

		public static readonly DependencyProperty PostRootIdProperty =
			DependencyProperty.Register
				(
					"PostRootId",
					typeof (ulong),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public ulong PostRootId
		{
			get { return (ulong) GetValue(PostRootIdProperty); }
			set { SetValue(PostRootIdProperty, value); }
		}

		#endregion

		#region IsThreadExpanded DependencyProperty

		public static readonly DependencyProperty IsThreadExpandedProperty =
			DependencyProperty.Register
				(
					"IsThreadExpanded",
					typeof (bool),
					typeof (ThreadControl),
					new PropertyMetadata(false, (s, e) =>
					{
						if (s is ThreadControl)
						{
							ThreadControl ctrl = s as ThreadControl;
							bool val = (bool) e.NewValue;
							if (val)
								VisualStateManager.GoToState(ctrl, "ThreadExpanded", false);
							else
								VisualStateManager.GoToState(ctrl, "ThreadCollapsed", false);
						}
					})
				);

		public bool IsThreadExpanded
		{
			get { return (bool) GetValue(IsThreadExpandedProperty); }
			set { SetValue(IsThreadExpandedProperty, value); }
		}

		#endregion

		#region IsMessageExpanded DependencyProperty

		public static readonly DependencyProperty IsMessageExpandedProperty =
			DependencyProperty.Register
				(
					"IsMessageExpanded",
					typeof (bool),
					typeof (ThreadControl),
					new PropertyMetadata(false, (s, e) =>
					{
						if (s is ThreadControl)
						{
							ThreadControl ctrl = s as ThreadControl;
							bool val = (bool) e.NewValue;
							if (val)
								VisualStateManager.GoToState(ctrl, "Expanded", false);
							else
								VisualStateManager.GoToState(ctrl, "Collapsed", false);
						}
					})
				);

		public bool IsMessageExpanded
		{
			get { return (bool) GetValue(IsMessageExpandedProperty); }
			set { SetValue(IsMessageExpandedProperty, value); }
		}

		#endregion

		#region IsSelected DependencyProperty

		public static readonly DependencyProperty IsSelectedProperty =
			DependencyProperty.Register
				(
					"IsSelected",
					typeof (bool),
					typeof (ThreadControl),
					new PropertyMetadata(null)
				);

		public bool IsSelected
		{
			get { return (bool) GetValue(IsSelectedProperty); }
			set { SetValue(IsSelectedProperty, value); }
		}

		#endregion
	}
}